//
//  YFShopCarHeaderView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFShopCarHeaderView.h"

@interface YFShopCarHeaderView()
//@property (nonatomic, strong) UIButton *selectedButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *arrowImgView;
@end
@implementation YFShopCarHeaderView


//- (instancetype)init
//{
//    self = [super init];
//    if (self) {
//        [self makeView];
//    }
//    return self;
//}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self makeView];
    }
    return self;
}


- (void)makeView {
    
    UIButton *selectedButton = [[UIButton alloc] init];
    [selectedButton addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [selectedButton setImage:[UIImage imageNamed:@"Snoselect"] forState:UIControlStateNormal];
    [selectedButton setImage:[UIImage imageNamed:@"Sselect"] forState:UIControlStateSelected];
    [self addSubview:selectedButton];
    self.selectedButton = selectedButton;
    

    CGFloat labelX = 20;
    CGFloat labelY = 20;
    UILabel *label = [[UILabel alloc] init];
//    label.text = @"";
    label.textColor = [UIColor colorWithHexString:@"333333"];
    label.font = [UIFont systemFontOfSize:13];
    [self addSubview:label];
//    label.backgroundColor = [UIColor blueColor];
    self.titleLabel = label;
    
    {
        CGFloat imgWH = UISCREEN_WIDTH;
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.image = [UIImage imageNamed:@"select"];
//        imgView.frame = CGRectMake(0, 0, imgWH, imgWH);
        [self addSubview:imgView];
        self.arrowImgView = imgView;
    }
    
    CGFloat lingH = 0.5;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, self.mj_h - lingH, self.mj_w, lingH)];
    line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:line];
    [self bringSubviewToFront:line];
    
    [self.selectedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(20));
        make.centerY.equalTo(self);
        make.left.equalTo(self).with.offset(10);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self);
        make.left.equalTo(self.selectedButton.mas_right).with.offset(10);
        make.width.equalTo(@(100));
    }];
    
    [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(15));
        make.centerY.equalTo(self);
        make.left.equalTo(self.titleLabel.mas_right).with.offset(10);
    }];
}

- (void)setModel:(YFShopCarModel *)model {
    _model = model;
    
    // 是否选中
    self.selectedButton.selected = model.isSelect.intValue;
    
    // 20 是两个间距
    self.titleLabel.text = model.shop_name;
    
    CGFloat width = self.arrowImgView.mj_x - CGRectGetMaxX(self.selectedButton.frame) - 20;
    CGSize size = [YFGlobalTool sizeWithString:self.titleLabel.text font:self.titleLabel.font maxSize:CGSizeMake(width, MAXFLOAT)];
    
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(size.width + 10));
    }];
    // 更新宽度
}



- (void)selectedButtonClick:(UIButton *)btn {
    btn.selected = !btn.selected;
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (btn.selected) {
        [dict setObject:@"1" forKey:@"selected"];
    }else{
        [dict setObject:@"0" forKey:@"selected"];
    }
   
    [dict setObject:self.model.shop_id forKey:@"shop_id"];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ShopSelectedOrDesSelectedNotification" object:dict];
    
}

- (void)setIsSelect:(BOOL)isSelect {
    _isSelect = isSelect;
    self.selectedButton.selected = isSelect;
}

- (void)updateState:(BOOL)isSelected {
    NSLog(@"selectedButton = %p", self.selectedButton);
    self.selectedButton.selected = isSelected;
}

@end
